package cn.edu.zjicm.wordsnet_d.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;
import cn.edu.zjicm.wordsnet_d.adapter.WordsBookPagerAdapter;
import cn.edu.zjicm.wordsnet_d.data.DataManager;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.datapersistent.FileManager;
import cn.edu.zjicm.wordsnet_d.util.Utils;
import cn.edu.zjicm.wordsnet_d.view.pageindicator.PageIndicator;

import com.umeng.analytics.MobclickAgent;

public class WordsBookActivity extends WordBaseActivity {

	public static void startActivity(Context context) {
		Intent i = new Intent(context, WordsBookActivity.class);
		context.startActivity(i);
	}

	public static void startActivityOnDifficultyWords(Context context) {
		Intent i = new Intent(context, WordsBookActivity.class);
		i.putExtra("isOnDifficulty", 1);
		context.startActivity(i);
	}

	public static ArrayList<OneWord> wordList = new ArrayList<OneWord>();
	public int currentIndex;
	public boolean edit = false;
	public int isOnDifficulty;
	private Context mContext;
	private ViewPager mWordUnitPager;
	public WordsBookPagerAdapter mPagerAdapter;
	private PageIndicator mPageIndicator;
	private Button startHuanboButton;
	private Button familiarSetButton;
	private CheckBox wordAllSelectCheckBox;
	private TextView selectCountTv;
	private LinearLayout huanboLayout, familiarSetLayout;
	public boolean showChineseInDifficult = true;

	private static WordsBookActivity instance;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		isOnDifficulty = getIntent().getIntExtra("isOnDifficulty", 0);
		// 设定调整音量为媒体音量
		setVolumeControlStream(AudioManager.STREAM_MUSIC);
		setTitleBarStyle(TITLE_BAR_STYLE_BACK_BUTTON);
		setTitleText("我的词汇本");
		setContentView(R.layout.activity_wordsbook);
		mContext = WordsBookActivity.this;
		findViews();
		initViews();
		init();
		instance = this;
	}

	private void findViews() {
		mWordUnitPager = (ViewPager) findViewById(R.id.word_unit_pager);
		mPageIndicator = (PageIndicator) findViewById(R.id.word_unit_pager_indicator);
		startHuanboButton = (Button) findViewById(R.id.start_huanbo_button);
		familiarSetButton = (Button) findViewById(R.id.familiar_set_button);
		huanboLayout = (LinearLayout) findViewById(R.id.huanbo_layout);
		familiarSetLayout = (LinearLayout) findViewById(R.id.familiar_set_layout);
		wordAllSelectCheckBox = (CheckBox) findViewById(R.id.word_all_select_checkbox);
		selectCountTv = (TextView) findViewById(R.id.select_count);
	}

	public void initViews() {
		if (AppPreference.getSortType(this, 1000) == 0)
			AppPreference.setSortType(this, 1000, -1);
		mPagerAdapter = new WordsBookPagerAdapter(this, mPageIndicator);
		mWordUnitPager.setAdapter(mPagerAdapter);
		mPageIndicator.setViewPager(mWordUnitPager);
		// mPageIndicator.setCurrentItem(0);

	}

	private void init() {

		wordAllSelectCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					mPagerAdapter.allSelect();
				} else {
					wordList.clear();
				}
				resetListForEdit(3);
			}
		});
		startHuanboButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (FileManager.hasNotDownloadedVoicePack(mContext, true))
					Toast.makeText(mContext, "有部分音频文件没有下载，可能会影响听音，建议去设置页面下载", Toast.LENGTH_LONG).show();
				else
					ExamHuanboActivity.startActivity(mContext, currentIndex);
			}
		});

		familiarSetButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				AlertDialog.Builder builder = new Builder(mContext);
				if (currentIndex == 1) {
					builder.setMessage("确定将选定的" + wordList.size() + "个单词设为太简单？");
				} else if (currentIndex == 2) {
					builder.setMessage("确定将选定的" + wordList.size() + "个单词设为生词？");
				} else if (currentIndex == 3) {
					builder.setMessage("确定将选定的" + wordList.size() + "个单词移除标记吗？移除后它们将不再在重难词列表中出现。");
				}
				builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						int num = wordList.size();
						if (currentIndex == 1) {
							while (wordList.size() > 0) {
								OneWord word = wordList.remove(0);
								OneWord w = DataManager.getInstance().findWord(word, mContext);
								DataManager.getInstance().changeFamiliarToTooEasy(w, mContext);
							}
							mPagerAdapter.mPagerAdapter.reLoadList();
							Toast.makeText(mContext, "已经将选定的" + num + "个单词设为太简单", Toast.LENGTH_SHORT).show();
						} else if (currentIndex == 2) {
							while (wordList.size() > 0) {
								OneWord word = wordList.remove(0);
								OneWord w = DataManager.getInstance().findWord(word, mContext);
								Log.e("Tag", "WordsBookActivity.w=" + w);
								DataManager.getInstance().changeTooEasyToUnFamiliar(w, mContext);
							}
							mPagerAdapter.mPagerAdapter.reLoadList();
							Toast.makeText(mContext, "已经将选定的" + num + "个单词设为生词", Toast.LENGTH_SHORT).show();
						} else if (currentIndex == 3) {
							while (wordList.size() > 0) {
								OneWord word = wordList.remove(0);
								word.setDifficulty(false, mContext);
							}
							mPagerAdapter.reLoadList();
							Toast.makeText(mContext, "已经将选定的" + num + "个单词取消标记难词", Toast.LENGTH_SHORT).show();
						}
						setFamiliarSetButton();
						dialog.dismiss();
					}
				});
				builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
				builder.create().show();
			}
		});
		setRightText2("编辑", 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				edit = !edit;
				setForEdit();
				resetListForEdit(currentIndex);
			}
		});
		setRightText("显示释义", new OnClickListener() {
			@Override
			public void onClick(View v) {
				showChineseInDifficult = !showChineseInDifficult;
				setForShowChinese();
				resetListForEdit(currentIndex);
			}
		});
		hideRightText();
		hideRightText2();
	}

	private void setForEdit() {
		if (edit) {
			setRightText2("完成");
			huanboLayout.setVisibility(View.GONE);
			familiarSetLayout.setVisibility(View.VISIBLE);
			wordList.clear();
			setFamiliarSetButton();
		} else {
			setRightText2("编辑");
			huanboLayout.setVisibility(View.VISIBLE);
			familiarSetLayout.setVisibility(View.GONE);
		}
	}

	public void setFamiliarSetButton() {
		String set = "";
		if (currentIndex == 1) {
			set = "设为太简单";
		} else if (currentIndex == 2) {
			set = "设为生词";
		} else if (currentIndex == 3) {
			set = "移除重难标记";
		}
		// if(currentIndex==3)
		// {
		// wordAllSelectCheckBox.setChecked(mPagerAdapter.isAllSelect());
		// }
		if (wordList.size() == 0) {
			familiarSetButton.setEnabled(false);
			familiarSetButton.setText(set);
			if (currentIndex == 3) {
				selectCountTv.setText("共" + wordList.size() + "个单词");
				wordAllSelectCheckBox.setChecked(false);
			}
		} else if (wordList.size() > 0) {
			familiarSetButton.setEnabled(true);
			if (currentIndex == 3) {
				selectCountTv.setText("共" + wordList.size() + "个单词");
				familiarSetButton.setText(set);
			} else {
				familiarSetButton.setText(set + "(" + wordList.size() + ")");
			}

		}
	}

	public void setCurrentViewPagerIndex(final int index) {
		if (this.currentIndex == index) {
			return;
		}
		if (edit) {
			edit = false;
		}
		this.currentIndex = index;
		resetListForEdit(currentIndex);
		setForEdit();
		if (index != 0) {
			showRightText2();
		} else {
			hideRightText2();
		}
		if (index == 3) {
			familiarSetButton.setPadding(Utils.changeDipToPx(mContext, 30), 0, Utils.changeDipToPx(mContext, 30), 0);
			wordAllSelectCheckBox.setVisibility(View.VISIBLE);
			selectCountTv.setVisibility(View.VISIBLE);
			setForShowChinese();
			showRightText();
		} else {
			familiarSetButton.setPadding(Utils.changeDipToPx(mContext, 50), 0, Utils.changeDipToPx(mContext, 50), 0);
			wordAllSelectCheckBox.setVisibility(View.GONE);
			selectCountTv.setVisibility(View.GONE);
			hideRightText();
		}
	}

	private void setForShowChinese() {
		if (showChineseInDifficult) {
			setRightText("隐藏释义");
		} else {
			setRightText("显示释义");
		}
	}

	private void resetListForEdit(int index) {
		if (index < 3) {
			mPagerAdapter.mPagerAdapter.resetListForEdit(index);
		} else if (index == 3) {
			mPagerAdapter.resetListForEdit();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	// 添加菜单
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}

	// 响应菜单
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		}
		return super.onOptionsItemSelected(item);
	}

	public void setCurrentPage(int index) {
		mWordUnitPager.setCurrentItem(index);
	}

	public static WordsBookActivity getInstance() {
		return instance;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != Activity.RESULT_OK) {
			return;
		}

		if (requestCode == WordDetailActivity.DIFFICULT_CHANGE_CODE) {

			mPagerAdapter.reLoadList();
		}
	}

}
